package com.itheima.controller.backend;

import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.common.entity.Result;
import com.itheima.pojo.CheckItem;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 检查项管理
 */
@RestController
@RequestMapping( "/checkitem")
@Slf4j
public class CheckItemController {

    @Autowired
    private com.itheima.service.backend.CheckItemService checkItemService;

    /**
     * 新增检查项
     * @param checkItem
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody CheckItem checkItem){
        log.info("新增检查项：{}", checkItem);
        try {
            checkItemService.add(checkItem);
            return new Result(true, "新增检查项成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "新增检查项失败");
        }
    }

    /**
     * 删除检查项
     * @param id
     * @return
     */
    @GetMapping("/delete")
    public Result delete(Integer id){
        log.info("删除检查项：{}", id);
        try {
            return checkItemService.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "删除检查项失败，请确认！");
        }
    }

    /**
     * 根据检查组id查询检查项id
     * @param checkgroupId
     * @return
     */
    @GetMapping("/findCheckItemIdsByCheckGroupId")
    public Result findCheckItemIdsByCheckGroupId(Integer checkgroupId) {
        try {
            return new Result(true, "查询成功", checkItemService.findCheckItemIdsByCheckGroupId(checkgroupId));
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询失败");
        }
    }

    /**
     * 编辑检查项
     * @param checkItem
     * @return
     */
    @PostMapping("/edit")
    public Result edit(@RequestBody CheckItem checkItem){
        log.info("编辑检查项：{}", checkItem);
        try {
            return checkItemService.edit(checkItem);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "编辑检查项失败，请确认！");
        }
    }

    /**
     * 分页查询检查项
     * @param queryPageBean
     * @return
     */
    @PostMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        log.info("分页查询检查项：{}", queryPageBean);
        return checkItemService.findPage(queryPageBean);
    }

    /**
     * 查询所有检查项
     * @return
     */
    @GetMapping("/findAll")
    public Result findAll(){
        log.info("查询所有检查项");
        try {
            return new Result(true, "查询成功", checkItemService.findAll());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询失败");
        }
    }

    /**
     * 根据id查询检查项
     * @param id
     * @return
     */
    @GetMapping("/findById")
    public Result findById(Integer id){
        log.info("根据id查询检查项：{}", id);
        try {
            return new Result(true, "查询成功", checkItemService.findById(id));
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询失败");
        }
    }
}